package com.oocl.command;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

import com.oocl.dataprocess.comparator.BirthComparator;
import com.oocl.dataprocess.comparator.IdComparator;
import com.oocl.dataprocess.filter.Impl.FilterRex;
import com.oocl.dataprocess.parser.Impl.ParseTxT;
import com.oocl.dataprocess.pojo.Customer;
import com.oocl.dataprocess.printer.Printer;

public class Command {
	private  FilterRex fRex;
	private  ParseTxT pTXT;
	
	public Command(FilterRex fRex, ParseTxT pTXT) {
		super();
		this.fRex = new FilterRex();
		this.pTXT = new ParseTxT();
	}

	public  String getXMLById(){
		String printXML = Printer.printXML(fRex.filterWrong(pTXT.parseFromTxT("data.txt")),new IdComparator());
		return printXML;
	}
	public String getStringOrderByBirth(){
		ArrayList<Customer> customer2 = fRex.filterWrong(pTXT.parseFromTxT("data.txt"));
		String printBirth = Printer.print("birth", customer2);
		return printBirth;
	}
	public void createXMLFile(){
		OutputStream out;
		ArrayList<Customer> customers = fRex.filterWrong(pTXT.parseFromTxT("data.txt"));
		Set<Customer> set = new TreeSet<Customer>(new IdComparator());
		for(Customer customer: customers){
			set.add(customer);
		}
		try {
			out=new FileOutputStream("data.xml");
			out.write((Printer.printXML(fRex.filterWrong(pTXT.parseFromTxT("data.txt")),new IdComparator())).getBytes());
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
}
